<template>
  <div>
    <el-dialog
      title="用户信息"
      :visible.sync="dialogVisible"
      width="600px"
      destroy-on-close
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户昵称" prop="username">
              <el-input v-model="form.username" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="logincode" prop="logincode">
              <el-input v-model="form.logincode" maxlength="30" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="手机号码" prop="phone">
              <el-input v-model="form.phone" maxlength="11" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="角色">
              <el-select v-model="form.role.id" filterable clearable>
                <el-option
                  v-for="(item, indx) in roleOptions"
                  :key="indx"
                  :label="item.name + '-' + item.code"
                  :value="item.id"
                  :disabled="item.state == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                v-model="form.snote"
                type="textarea"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { roleService, userService } from "@/api/sysService";

export default {
  dicts: ["sys_normal_disable", "sys_user_sex"],
  components: {},

  data() {
    return {
      resolve: null,
      dialogVisible: false,

      deptOptions: [],
      postOptions: [],
      roleOptions: [],

      // 表单参数
      form: {
        id: "",
        username: undefined,
        logincode: undefined,
        phone: undefined,
        role: { id: "" },
        snote: "",
      },

      // 表单校验
      rules: {
        username: [
          { required: true, message: "不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        logincode: [
          { required: true, message: "不能为空", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9]+$/,
            message: "合法字符：大小写字母,数字",
            trigger: "blur",
          },
          {
            min: 8,
            max: 20,
            message: "长度必须介于 8 和 20 之间",
            trigger: "blur",
          },
        ],
        // password: [
        //   { required: true, message: "用户密码不能为空", trigger: "blur" },
        //   {
        //     min: 5,
        //     max: 20,
        //     message: "用户密码长度必须介于 5 和 20 之间",
        //     trigger: "blur",
        //   },
        //   {
        //     pattern: /^[^<>"'|\\]+$/,
        //     message: "不能包含非法字符：< > \" ' \\\ |",
        //     trigger: "blur",
        //   },
        // ],
        email: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
    };
  },

  created() {
    roleService.list({}).then((res) => {
      this.roleOptions = res.data.data;
    });
  },
  methods: {
    open(row) {
      // const data1 = { username: "John", phone: "123456789" };
      // const { username, phone, role = { id: "1" } } = data1;
      // console.log("test", { username, phone, role });

      console.log("userdialog");
      this.dialogVisible = true;
      if (row) {
        const { username, logincode, phone, role = {}, snote, id } = row;

        ///
        // {username: 'www', id: '', phone: '24', }
        // console.log({ username, logincode, phone, role, snote });
        this.form = {
          id,
          username,
          logincode,
          phone,
          role: { id: role?.id ?? "" },
          snote,
        };
      }
      return new Promise((resolve) => {
        this.resolve = resolve;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          userService.postSave(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.resolve();
            // this.getList();
          });
        }
      });
    },

    // 取消按钮
    cancel() {
      this.dialogVisible = false;
      this.reset();
    },
  },
};
</script>

<style lang="scss" scoped></style>
